package com.woniu.market_stall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.market_stall.entity.Market;
import com.woniu.market_stall.mapper.LogMapper;
import com.woniu.market_stall.mapper.MarketMapper;
import com.woniu.market_stall.vo.LogVo;
import com.woniu.market_stall.vo.MarketVo;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service

public class MarketService {

    @Autowired
    LogMapper logMapper;

     @Autowired
    MarketMapper marketMapper;

     public List<Market> queryAll(){
         return marketMapper.selectAll();
     }

   public PageInfo<MarketVo> queryMarketByCons(int now , int size ,  String marketName, String status){
       PageHelper.startPage(now , size);
       List<MarketVo> marketVos = marketMapper.selectMarketByCons(marketName, status);
       return new PageInfo<>(marketVos);
   }

    public int del(int id) {
        int i =  marketMapper.selectStatusById(id);
        // 状态为1-未开放，可删除，其他不可删除
        if (i == 1){
            marketMapper.deleteMarketById(id);
            return 1;
        }else {
            return 0;
        }

    }

    @Transactional
    public void add(MarketVo marketVo) {
        LogVo l = new LogVo();
        l.setManagerId(marketVo.getManagerId());
        l.setContent("执行了添加市场");
        l.setInsertTime(new Date());
        logMapper.addLog(l);

        marketMapper.insertMarket(marketVo);
    }
    @Transactional
    public void modify(MarketVo marketVo) {
        LogVo l = new LogVo();
        l.setManagerId(marketVo.getManagerId());
        l.setContent("执行了修改市场");
        l.setInsertTime(new Date());
        logMapper.addLog(l);
        marketMapper.updateMarketById(marketVo);
    }
}
